#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define uint unsigned int

#define YES cout << "YES" << '\n'
#define NO cout << "NO" << '\n'
#define endl '\n'
const int INF = 1e9;
const int MOD = 998244353;
using namespace std;

/*思考

*/
void solve() {
    int n, k;
    ll ans = 0;
    cin >> n >> k;
    vector<ll> a(n + 1), cnt(n + 1, 0), c(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        ++cnt[a[i]];
    }
    for (int i = 1; i <= n; ++i) {
        if (cnt[i] % k) {
            cout << 0 << endl;
            return;
        } else {
            cnt[i] /= k;
        }
    }
    for (int l = 1, r = 1; r >= l && r <= n; r++) {
        c[a[r]]++;
        while (c[a[r]] > cnt[a[r]]) {
            c[a[l]]--;
            l++;
        }
        ans += (r - l + 1);
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 0;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}